package com.baibei.ebec.home.newIndex;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.model.ProductInfo;
import com.baibei.module.ImageUtils;
import com.baibei.module.OnItemSelectListener;
import com.baibei.module.ViewUtil;
import com.blankj.utilcode.utils.SizeUtils;
import com.blankj.utilcode.utils.Utils;
import com.bumptech.glide.Glide;
import com.rae.swift.Rx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ChenZhenGuang on 2018/5/15/007.
 *
 * @email chenzhenguang@landsone.com
 */

public class ProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ProductInfo> mInfos;
    private OnItemSelectListener<ProductInfo> mItemSelectListener;
    private int clickPostion = 0; // 当前点击的


    public ProductAdapter(OnItemSelectListener<ProductInfo> mItemSelectListener) {
        this.mItemSelectListener = mItemSelectListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ProductInfo info = mInfos.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.tvName.setText(info.getName());
        viewHolder.tvBookPrice.setText(String.format("¥%s", Rx.formatPrice(info.getPrice())));
        int imageSize = SizeUtils.dp2px(65);
        String imageUrl = ImageUtils.getImageUrlBySize(info.getProductPic(), imageSize, imageSize);
        Glide.with(holder.itemView.getContext()).load(imageUrl)
//                .transform(new RoundedCorners(SizeUtils.dp2px(5)))
                .into(viewHolder.ivLogo);
        viewHolder.imgStatus.setVisibility(!TextUtils.isEmpty(info.getTicketId()) ? View.VISIBLE : View.GONE);

        ViewUtil.setViewSize(viewHolder.linRoot, (int) (ViewUtil.getScreenWidth(Utils.getContext()) / 3.4), (int) (ViewUtil.getScreenWidth(Utils.getContext()) / 3.4));

        if (position == clickPostion) {
            viewHolder.linRoot.setEnabled(false);
        } else {
            viewHolder.linRoot.setEnabled(true);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickPostion = position;
                notifyDataSetChanged();
                if (mItemSelectListener != null) {
                    mItemSelectListener.onItemSelectListener(v, info, position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    public int getClickPostion(){
        return clickPostion;
    }

    void setData(List<ProductInfo> productInfos) {
        clickPostion = 0;
        if (!productInfos.equals(mInfos)) {
            mInfos = productInfos;
            notifyDataSetChanged();
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.img_status)
        ImageView imgStatus;
        @BindView(R2.id.iv_logo)
        ImageView ivLogo;
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_book_amount)
        TextView tvBookPrice;
        @BindView(R2.id.lin_root)
        LinearLayout linRoot;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
